/*
 * Copyright By Peak3
 * Copyright By c336bf6ff03a756989bb456b5b5e3271228d43ed38461eb5cb5755d17a2fc9b8
 * All rights reserved.
 *
 * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
 * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your system.
 */

package com.vv.tools.modules.utils.web.controller;

import com.vv.tools.common.message.Result;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * CORS跨域测试控制器
 * 
 * @author liwen
 */
@RestController
@RequestMapping("/utils/cors")
public class CorsTestController {

    /**
     * GET请求测试
     */
    @GetMapping("/test")
    public Result<Map<String, Object>> testGet() {
        Map<String, Object> data = new HashMap<>();
        data.put("method", "GET");
        data.put("timestamp", LocalDateTime.now());
        data.put("message", "CORS GET 请求测试成功");
        data.put("cors", "enabled");
        
        return Result.success(data);
    }

    /**
     * POST请求测试
     */
    @PostMapping("/test")
    public Result<Map<String, Object>> testPost(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> data = new HashMap<>();
        data.put("method", "POST");
        data.put("timestamp", LocalDateTime.now());
        data.put("message", "CORS POST 请求测试成功");
        data.put("requestData", requestData);
        data.put("cors", "enabled");
        
        return Result.success(data);
    }

    /**
     * OPTIONS预检请求测试
     */
    @RequestMapping(value = "/test", method = RequestMethod.OPTIONS)
    public Result<Map<String, Object>> testOptions() {
        Map<String, Object> data = new HashMap<>();
        data.put("method", "OPTIONS");
        data.put("timestamp", LocalDateTime.now());
        data.put("message", "CORS OPTIONS 预检请求测试成功");
        data.put("cors", "enabled");
        
        return Result.success(data);
    }

    /**
     * 获取CORS配置信息
     */
    @GetMapping("/config")
    public Result<Map<String, Object>> getCorsConfig() {
        Map<String, Object> config = new HashMap<>();
        config.put("allowCredentials", true);
        config.put("maxAge", 3600);
        config.put("supportedMethods", new String[]{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"});
        config.put("note", "具体允许的源地址根据环境配置确定");
        config.put("timestamp", LocalDateTime.now());
        
        return Result.success(config);
    }

    /**
     * PUT请求测试
     */
    @PutMapping("/test")
    public Result<Map<String, Object>> testPut(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> data = new HashMap<>();
        data.put("method", "PUT");
        data.put("timestamp", LocalDateTime.now());
        data.put("message", "CORS PUT 请求测试成功");
        data.put("requestData", requestData);
        data.put("cors", "enabled");
        
        return Result.success(data);
    }

    /**
     * DELETE请求测试
     */
    @DeleteMapping("/test")
    public Result<Map<String, Object>> testDelete() {
        Map<String, Object> data = new HashMap<>();
        data.put("method", "DELETE");
        data.put("timestamp", LocalDateTime.now());
        data.put("message", "CORS DELETE 请求测试成功");
        data.put("cors", "enabled");
        
        return Result.success(data);
    }
}
